/**
 * @Copyright: Interactive Business Solution Pte Ltd, Singapore
 * @Author: Dolita, 2007-12-22
 * @Version: 071222.001
 * Data access object of Flight model
 */
package com.ibsol.database
{
	import com.ibsol.model.Flight;
	
	import flash.data.SQLConnection;
	import flash.data.SQLStatement;
	import flash.events.SQLErrorEvent;
	import flash.events.SQLEvent;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	
	public class FlightDAO
	{
		private var _tourixDBConn:SQLConnection;
		/**
		 * Default constructor of Flight DAO, get database connection
		 */
		public function FlightDAO()
		{
			_tourixDBConn = DBConnManager.getConnection("tourix");
		}
		/**
		 * Insert data into flight table
		 * flight(id, packageid, airline, aircraft, flightNo, departureDate, arrivalDate, duration,
		 * start, destination, logo, economy, business, first, offlineOperator, updateTime)
		 * @param: flight:Flight			flight item object which will be populating flight table
		 * 		   resultHandler:Function	result function adapter
		 * 		   faultHandler:Function	fault function adapter
		 * @return: void
		 */
		public function insertFlight(flight:Flight, resultHandler:Function = null, faultHandler:Function = null):void
		{
			var sqlStr:SQLStatement = new SQLStatement();
			sqlStr.sqlConnection = _tourixDBConn;
			sqlStr.text = "INSERT INTO flight (id, packageid, airline, aircraft, flightNo, departureDate, arrivalDate, duration, start, destination, logo, economy, business, first, offlineOperator, updateTime) " + 
						"VALUES (" + flight.id + "," + flight.packageid + ", '" + flight.airline + "', '" + flight.aircraft + 
						"', '" + flight.flightNo + "', '" + flight.departureDate + "', '" + flight.arrivalDate + "', '" + flight.duration +
						"', '" + flight.start + "', '" + flight.destination + "', '" + flight.logo + "', " + flight.economy + "," + flight.business + 
						"," + flight.first + ", 'NEW', 'T')";
			sqlStr.addEventListener(SQLEvent.RESULT,
				function(event:SQLEvent):void
				{
					if(resultHandler != null)
						resultHandler.call(this);
				});
			sqlStr.addEventListener(SQLErrorEvent.ERROR, faultHandler == null ? sqlErrorHandler : faultHandler);
			sqlStr.execute();
		}							
		/**
		 * Load flight according to package id
		 * @param:	packageid:int			package's id, flight belongs to
		 * 			resultHandler:Function	result function adapter, return flight item array collection
		 * 			faultHandler:Function	fault function adapter
		 * @return: void
		 */
		public function loadFlightsByPackage(packageid:int, resultHandler:Function, faultHandler:Function = null):void
		{
			var sqlStr:SQLStatement = new SQLStatement();
			sqlStr.sqlConnection = _tourixDBConn;
			sqlStr.text = "SELECT * FROM flight WHERE packageid = " + packageid + " AND offlineOperator != 'DEL'";
			sqlStr.addEventListener(SQLEvent.RESULT,
				function (event:SQLEvent):void
				{
					resultHandler.call(this, typeArray(sqlStr.getResult().data));
				});
			sqlStr.addEventListener(SQLErrorEvent.ERROR, faultHandler == null ? sqlErrorHandler : faultHandler);
			sqlStr.execute();
		}
		/**
		 * Updates a flight record in database. For different level of user, there are different options.
		 * flight(id, packageid, airline, aircraft, flightNo, departureDate, arrivalDate, duration,
		 * start, destination, logo, economy, business, first, offlineOperator, updateTime)
		 * @param:	flight:Flight			flight item object which will be updated
		 * 			resultHandler:Function	result handler function
		 * 			faultHandler:Function	fault handler function
		 * @return: void
		 */	
		public function updateFlight(flight:Flight, resultHandler:Function = null, faultHandler:Function = null):void
		{
			var sqlStr:SQLStatement = new SQLStatement();
			sqlStr.sqlConnection = _tourixDBConn;
			sqlStr.text = "UPDATE flight SET packageid=" + flight.packageid + ",airline='" + flight.airline + "',aircraft='" + flight.aircraft + 
					"',flightNo='" + flight.flightNo + "',departureDate='" + flight.departureDate + "',arrivalDate='" + flight.arrivalDate + 
					"',duration='" + flight.duration + "',start='" + flight.start + "',destination='" + flight.destination + "',logo='" + flight.logo +
					"',economy=" + flight.economy + ",business=" + flight.business + ",first=" + flight.first + 
					",offlineOperator = 'EDIT',updateTime='T' WHERE id = " + flight.id;
			sqlStr.addEventListener(SQLEvent.RESULT,
				function(event:SQLEvent):void
				{
					if(resultHandler != null)
						resultHandler.call(this);
				});
			sqlStr.addEventListener(SQLErrorEvent.ERROR, faultHandler == null ? sqlErrorHandler : faultHandler);
			sqlStr.execute();
		}
		/**
		 * Permanently delete flight from database, whose offlineOperator is set to "DEL". If user wants to delete flight, 
		 * user just updates offlineOperator to "DEL".
		 * @param:	resultHandler:Function		result handler adapter
		 * 			faultHandler:Function		fault handler adapter
		 * @return:	void
		 */
		public function clearDeletedFlights(resultHandler:Function = null, faultHandler:Function = null):void
		{
			var sqlStr:SQLStatement = new SQLStatement();
			sqlStr.sqlConnection = _tourixDBConn;
			sqlStr.text = "DELETE FROM flight WHERE offlineOperator = 'DEL'";
			sqlStr.addEventListener(SQLEvent.RESULT,
				function (event:SQLEvent):void
				{
					if(resultHandler != null)
						resultHandler.call(this);
				});
			sqlStr.addEventListener(SQLErrorEvent.ERROR, faultHandler == null ? sqlErrorHandler : faultHandler);
			sqlStr.execute();
		}
		/**
		 * Sets offline operator to "", this function called after synchronization with server
		 * @param:	resultHandler:Function		result handler adapter
		 * 			faultHandler:Function		fault handler adapter
		 * @return: void
		 */
		public function resetOfflineOperator(resultHandler:Function = null, faultHandler:Function = null):void
		{
			var sqlStr:SQLStatement = new SQLStatement();
			sqlStr.sqlConnection = _tourixDBConn;
			sqlStr.text = "UPDATE flight SET offlineOperator=''";
			sqlStr.addEventListener(SQLEvent.RESULT,
				function (event:SQLEvent):void
				{
					if(resultHandler != null)
						resultHandler.call(this);
				});
			sqlStr.addEventListener(SQLErrorEvent.ERROR, faultHandler == null ? sqlErrorHandler : faultHandler);
			sqlStr.execute();
		}
		/**
		 * Converts database object to Flight class
		 * @param:	o:Object	database return object
		 * @return:	Flight		Flight object after conversion
		 */
		private function typeObject(o:Object):Flight
		{
			var f:Flight = new Flight();
			f.id = o.id;
			f.packageid = o.packageid;
			f.airline = o.airline;
			f.aircraft = o.aircraft;
			f.flightNo = o.flightNo;
			f.departureDate = o.departureDate;
			f.arrivalDate = o.arrivalDate;
			f.duration = o.duration;
			f.start = o.start;
			f.destination = o.destination;
			f.logo = o.logo;
			f.economy = o.economy;
			f.business = o.business;
			f.first = o.first;
			f.offlineOperator = o.offlineOperator;
			f.updateTime = o.updateTime;
			
			return f;
		}
		/**
		 * Converts array object to Flight array collection
		 * @param: a:Array array of objects returned by SQL
		 * @return: ArrayCollection of Flight after conversion from Array
		 */
		private function typeArray(a:Array):ArrayCollection
		{
			if (a==null) return null;
			var ac:ArrayCollection = new ArrayCollection();
			for (var i:int; i<a.length; i++)
			{
				ac.addItem(typeObject(a[i]));
			}
			return ac;
		}
		/**
		 * Default SQL error handler, if above functions' fault handlers are null, this error handler is applied
		 * @param: event:SQLError	SQL Error event will be handled if there is an error
		 * @return: void
		 */
		private function sqlErrorHandler(event:SQLErrorEvent):void
		{
			mx.controls.Alert.show(event.text, "Error");
			trace(event.text);
		}
	}
}